var app = getApp()
var base64 = require('../../utils/base64.js');
Page({
  data: {
    glist: '',
    count_price: '',
    v_price: '',
    shop_id: '',
    voucher_ids: 0,
    keys: '',
    voucher_list: '',
    typ: ''
  },
  onLoad: function (options) {
    var that = this
    if (options.typ == 'ziti') {
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];   //当前页面
      var prevPage = pages[pages.length - 2];
      var voucher_list = prevPage.data.order.voucherList
      var voucher_ids = prevPage.data.voucherId
     
      that.setData({
        voucher_list: voucher_list,
        voucher_ids: voucher_ids,
        typ: 'ziti'
      })
    } else {
      var glist = JSON.parse(base64.decode(decodeURIComponent(options.glist)))
      var count_price = options.count_price
      var v_price = options.v_price
      var voucher_ids = options.voucher_ids
      if (glist) {
        if (glist.voucher_base && !voucher_ids) {
          for (var j in glist.voucher_base) {
            if (glist.voucher_base[j].id == glist.best_voucher[0].id) {
              voucher_ids = glist.voucher_base[j].id
            }
          }
        }

        that.setData({
          glist: glist,
          voucher_list: glist.voucher_base,
          count_price: count_price,
          v_price: v_price,
          shop_id: options.shop_id,
          voucher_ids: voucher_ids,
          keys: options.keys,

        })
      }
    }

  },
  //门店自提订单代金券选择
  receives: function (e) {
    var that = this
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];
    var voucherDiscountMsg = '';
    var id = e.currentTarget.dataset.id;
    var voucher_limit = e.currentTarget.dataset.voucher_limit;
    var voucher_price = e.currentTarget.dataset.voucher_price;
    if (id == 0) {
      if (prevPage.data.redPacketId > 0){
        var discountedOrderAmount = Number(prevPage.data.orderAmount) - Number(prevPage.data.redPacketDiscount)
      }else{
        var discountedOrderAmount = Number(prevPage.data.orderAmount)
      }
      var isUserOptOutVoucher = 1
      voucherDiscountMsg = '不使用店铺代金券'
      
    } else {
      if (prevPage.data.redPacketId > 0){
        var discountedOrderAmount = Number(prevPage.data.orderAmount) - Number(voucher_price) - Number(prevPage.data.redPacketDiscount)
      }else{
        var discountedOrderAmount = Number(prevPage.data.orderAmount) - Number(voucher_price)
      }
      var isUserOptOutVoucher = 0
      voucherDiscountMsg = "满" + voucher_limit + "减" + voucher_price;
    }


    that.setData({
      voucher_ids: id
    })
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      discountedOrderAmount: discountedOrderAmount,
      isUserOptOutVoucher: isUserOptOutVoucher,
      voucherDiscountMsg: voucherDiscountMsg,
      voucherDiscount: voucher_price,
      voucherId: id
    })
    wx.navigateBack({
      delta: 1
    })
  },
  //代金券选择
  receive: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id

    var limit = e.currentTarget.dataset.limit
    var price = e.currentTarget.dataset.price
    if (id == 0) {
      var order_price = Number(that.data.count_price) + Number(that.data.v_price)
    } else {
      var order_price = Number(that.data.count_price) + Number(that.data.v_price) - Number(price)
    }
    var shop_id = that.data.shop_id
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];
    var voucher_list = prevPage.data.voucher_id
    var glistAll = prevPage.data.glistAll
    for (var j in glistAll) {
      if (glistAll[j].shop_id == that.data.shop_id && id != 0) {
        glistAll[j].best_voucher_info = '满' + limit + '减' + price
        voucher_list[that.data.keys] = id
        glistAll[j].best_voucher[0].price = price
        glistAll[j].best_voucher[0].limit = limit
      } else {
        glistAll[j].best_voucher_info = '不使用店铺代金券'
        voucher_list[that.data.keys] = 0
        glistAll[j].best_voucher[0].price = ''
        glistAll[j].best_voucher[0].limit = ''
      }
    }

    that.setData({
      voucher_ids: id
    })
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      count_price: order_price,
      glistAll: glistAll,
      voucher_id: voucher_list
    })
    wx.navigateBack({
      delta: 1
    })


  }
})